package generater.executor.config;

import cn.hutool.core.util.StrUtil;
import waiter.core.util.ToolUtil;
import generater.executor.model.GenQuery;

import com.baomidou.mybatisplus.generator.config.rules.DbType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.io.File;

/**
 * 默认的代码生成的配置
 *
 * 
 * @date 20107年12月31日
 */
public class WebGeneratorConfig extends AbstractGeneratorConfig {

    private GenQuery genQuery;

    public WebGeneratorConfig(GenQuery genQuery) {
        this.genQuery = genQuery;
    }

    @Override
    protected void config() {
        /**
         * 数据库配置
         */
        dataSourceConfig.setDbType(DbType.MYSQL);
        dataSourceConfig.setDriverName("com.mysql.cj.jdbc.Driver");
        dataSourceConfig.setUsername(genQuery.getUserName());
        dataSourceConfig.setPassword(genQuery.getPassword());
        dataSourceConfig.setUrl(genQuery.getUrl());

        /**
         * 全局配置
         */
        globalConfig.setOutputDir(genQuery.getProjectPath() + File.separator + "src" + File.separator + "main" + File.separator + "java");
        globalConfig.setFileOverride(true);
        globalConfig.setEnableCache(false);
        globalConfig.setBaseResultMap(true);
        globalConfig.setBaseColumnList(true);
        globalConfig.setOpen(false);
        globalConfig.setAuthor(genQuery.getAuthor());
        contextConfig.setProPackage(genQuery.getProjectPackage());
        contextConfig.setCoreBasePackage(genQuery.getCorePackage());

        /**
         * 生成策略
         */
        if (genQuery.getIgnoreTabelPrefix() != null) {
            strategyConfig.setTablePrefix(new String[]{genQuery.getIgnoreTabelPrefix()});
        }
        strategyConfig.setInclude(new String[]{genQuery.getTableName()});
        strategyConfig.setNaming(NamingStrategy.underline_to_camel);
        packageConfig.setParent(null);
        packageConfig.setEntity(genQuery.getProjectPackage() + ".modular.system.model");
        packageConfig.setMapper(genQuery.getProjectPackage() + ".modular.system.dao");
        packageConfig.setXml(genQuery.getProjectPackage() + ".modular.system.dao.mapping");

        /**
         * 业务代码配置
         */
        contextConfig.setBizChName(genQuery.getBizName());
        contextConfig.setModuleName(genQuery.getModuleName());
        contextConfig.setProjectPath(genQuery.getProjectPath());//写自己项目的绝对路径
        if(ToolUtil.isEmpty(genQuery.getIgnoreTabelPrefix())){
            String entityName = StrUtil.toCamelCase(genQuery.getTableName());
            contextConfig.setEntityName(StrUtil.upperFirst(entityName));
            contextConfig.setBizEnName(StrUtil.lowerFirst(entityName));
        }else{
            String entiyName = StrUtil.toCamelCase(StrUtil.removePrefix(genQuery.getTableName(), genQuery.getIgnoreTabelPrefix()));
            contextConfig.setEntityName(StrUtil.upperFirst(entiyName));
            contextConfig.setBizEnName(StrUtil.lowerFirst(entiyName));
        }
        sqlConfig.setParentMenuName(genQuery.getParentMenuName());//这里写已有菜单的名称,当做父节点

        /**
         * mybatis-plus 生成器开关
         */
        contextConfig.setEntitySwitch(genQuery.getEntitySwitch());
        contextConfig.setDaoSwitch(genQuery.getDaoSwitch());
        contextConfig.setServiceSwitch(genQuery.getServiceSwitch());

        /**
         * contextConfig生成器开关
         */
        contextConfig.setControllerSwitch(genQuery.getControllerSwitch());
        contextConfig.setIndexPageSwitch(genQuery.getIndexPageSwitch());
        contextConfig.setAddPageSwitch(genQuery.getAddPageSwitch());
        contextConfig.setEditPageSwitch(genQuery.getEditPageSwitch());
        contextConfig.setJsSwitch(genQuery.getJsSwitch());
        contextConfig.setInfoJsSwitch(genQuery.getInfoJsSwitch());
        contextConfig.setSqlSwitch(genQuery.getSqlSwitch());
    }
}
